package com.yuexunit.mobilenurse.module.PatientDetail.model.impl;

import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.PatientDetail.model.IPatientDetailModel;
import com.yuexunit.mobilenurse.util.CreateJson;
import com.yuexunit.mobilenurse.util.ProUtil;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.slf4j.Logger;

import java.text.SimpleDateFormat;
import java.util.Date;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * Created by sslcjy on 16/1/24.
 */
public class PatientDetailModel implements IPatientDetailModel {

    //记录Log
    private final Logger log = ProUtil.getLogger(PatientDetailModel.class);

    @Override
    public Observable<Object> getPatientDetail(final String Visitno) {

        return Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                try {
                    String nameSpace = AppConfig.WEB_NAME_SPACE;
                    String methodName = AppConfig.WEB_NAME_METHOD;
                    String url = AppConfig.WEB_CONTENT;
                    SoapObject request = new SoapObject(nameSpace, methodName);
                    request.addProperty("XmlString", CreateJson.Patinfo_Json(Visitno));
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                            SoapEnvelope.VER11);
                    envelope.bodyOut = request;
                    envelope.dotNet = true; //very important for compatibility
                    HttpTransportSE ht = new HttpTransportSE(url);
                    Object response = null;
                    try {
                        ht.call(nameSpace + methodName, envelope);
                        response = envelope.getResponse();

                        //记录传输到服务器及传回的数据
                        log.debug("getPatientDetail:  " + CreateJson.LOG_JSON(url + ",mhs_patinfo", "2", Visitno, response.toString()));
                    } catch (Exception e) {
                        log.debug("getPatientDetail:" + e);
                        e.printStackTrace();
                    }
                    subscriber.onNext(response);
                    subscriber.onCompleted();
                } catch (Exception e) {
                    subscriber.onError(e);
                    log.error("getPatientDetail_Exception:", e.toString());
                }
            }
        });
    }


    @Override
    public Observable collect(final String Visitno, final String Code, final String NurseId) {
        return Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                String nameSpace = AppConfig.WEB_NAME_SPACE;
                String methodName = AppConfig.WEB_NAME_METHOD;
                String url = AppConfig.WEB_CONTENT;
                SoapObject request = new SoapObject(nameSpace, methodName);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",java.util.Locale.getDefault());
                Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                String str = formatter.format(curDate);
                request.addProperty("XmlString", CreateJson.Ascollect_Json(Visitno, Code,
                        Integer.valueOf(NurseId), str, AppConfig.EXECTYPE));

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.bodyOut = request;
                envelope.dotNet = true; //very important for compatibility
                HttpTransportSE ht = new HttpTransportSE(url, AppConfig.TIMEOUT);
                Object response = null;
                try {
                    ht.call(nameSpace + methodName, envelope);
                    response = envelope.getResponse();
                    //记录传输到服务器及传回的数据
                    log.info(CreateJson.LOG_JSON(url + ",mhs_as_collect", "2",
                            Visitno + "," + Code + "," + Integer.valueOf(NurseId) + "," + str + "," + AppConfig.EXECTYPE
                            , response.toString()));
                    if (response == null) {
                        subscriber.onError(new Exception());
                    }
                } catch (Exception e) {
                    log.error("Exception:", e.toString());
                    e.printStackTrace();
                    subscriber.onError(e);
                }
                subscriber.onNext(response);
                subscriber.onCompleted();
            }
        }).map(new Func1<Object, String>() {
            @Override
            public String call(Object response) {
                return (String) response.toString();
            }
        });
    }
}

